package com.guge.sports.utils;

import com.guge.sports.annotation.EntityName;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * @author peng
 * @create 2022/9/15 9:23
 */
public class HashMapConvertUtil {

    public static <T> T convert2Entity(HashMap<String,Object> map, Class<T> cls){
        T t;
        try {
            Constructor<T> declaredConstructor = cls.getDeclaredConstructor();
            t = declaredConstructor.newInstance();
            for (Field declaredField : cls.getDeclaredFields()) {
                declaredField.setAccessible(true);
                String fieldName =declaredField.getName();
                //String simpleName = declaredField.getType().getSimpleName();
                if (declaredField.isAnnotationPresent(EntityName.class)) {
                    //字段上有注解
                    fieldName=declaredField.getAnnotation(EntityName.class).value();
                }
                //参数
                Object value = map.get(fieldName);
                if (value == null || "".equals(value)) {
                    continue;
                }
                declaredField.set(t, value);
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
       /* Method[] methods = cls.getDeclaredMethods();
        map.keySet().forEach((k)->{
            String key = (String) k;
            try {
                for (Method method : methods) {
                    if (method.getName().equals((String) "set" + key.substring(0, 1).toUpperCase() + key.substring(1))){
                        method.invoke(t,map.get(key));
                    }
                }
            }  catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        });*/

        return t;
    }


    /**
     *
     * @param parameter   参数名
     * @param simpleName  类型名
     * @return
     */
    private Object handleValue(String parameter, String simpleName) {
        Object value =new Object();
        if ("Integer".equals(simpleName)) {
            value = Integer.valueOf(parameter);
        } else if ("Float".equals(simpleName)) {
            value = Float.valueOf(parameter);
        } else if ("Date".equals(simpleName)) {
            value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(parameter);
        } else if ("Long".equals(simpleName)) {
            value = Long.valueOf(parameter);
        } else if ("Double".equals(simpleName)) {
            value = Double.valueOf(parameter);
        } else if ("String".equals(simpleName)) {
            value = parameter;
        }
        return value;
    }
}
